//
//  UITableView+LLAdd.m
//  WFTravel
//
//  Created by admin on 2018/4/13.
//  Copyright © 2018年 lg. All rights reserved.
//

#import "UITableView+LLAdd.h"
#import <objc/runtime.h>
#import <AFNetworking/AFNetworking.h>
#import "LLNetworkManagerTool.h"

static NSString *appRequestErrorWithInterntOffline = @"网络未连接，请查看网络是否链接正常！";;
static NSString *appRequestErrorWithOtherReason = @"暂无数据";

@implementation UITableView (LLAdd)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        [LLMethodSwizzTool methodSwizzWithClass:self OriginSEL:@selector(initWithFrame:style:) swizzSEL:@selector(ll_initWithFrame:style:)];
        
    });
}


- (instancetype)ll_initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    UITableView *tableView = [self ll_initWithFrame:frame style:style];
    
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.emptyDataSetDelegate = self;
    tableView.emptyDataSetSource = self;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.showsVerticalScrollIndicator = NO;
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusNotReachable) {
            self.emptyTitel = appRequestErrorWithInterntOffline;
        } else if (status == AFNetworkReachabilityStatusReachableViaWiFi){
//            self.emptyTitel = appRequestErrorWithOtherReason;
        }
    }];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    return tableView;
}


// 数据为空的时候，会执行
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.emptyTitel.length == 0) {
        return nil;
    }
    
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:self.emptyTitel attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : UIColorFromRGB(666666)}];
    return str;
}

//- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
//{
//    NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"数据空了" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]}];
//    return str;
//}

// 数据为空的时候，会执行
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.emptyImageName.length > 0 && ![self.emptyTitel isEqualToString:appRequestErrorWithInterntOffline]) {
        return [UIImage imageNamed:self.emptyImageName];
    }
    return [self.emptyTitel isEqualToString:appRequestErrorWithInterntOffline] ? [UIImage imageNamed:@""] : [UIImage imageNamed:self.emptyImageName];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return self.verticalOffset;
}




// 按钮
//- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
//{
//    NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"重新加载" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : [UIColor greenColor]}];
//    return str;
//}

// 加载时做动画
//- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
//{
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
//    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
//    animation.duration = 0.25;
//    animation.cumulative = YES;
//    animation.repeatCount = MAXFLOAT;
//
//    return animation;
//}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

//- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
//{
//    return YES;
//}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
//    if ([self.emptyDataDelegate respondsToSelector:@selector(emptyDataSet:didTapView:)]) {
//        [self.emptyDataDelegate emptyDataSet:self didTapView:view];
//    }
    if ([self.emptyDataDelegate respondsToSelector:@selector(emptyDataSetReloadData:)]) {
        [self.emptyDataDelegate emptyDataSetReloadData:self];
    }
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
//    if ([self.emptyDataDelegate respondsToSelector:@selector(emptyDataSet:didTapButton:)]) {
//        [self.emptyDataDelegate emptyDataSet:self didTapButton:button];
//    }
    if ([self.emptyDataDelegate respondsToSelector:@selector(emptyDataSetReloadData:)]) {
        [self.emptyDataDelegate emptyDataSetReloadData:self];
    }
}


// 设置空数据时的提示标题（断网 或者 数据为空）
- (void)setEmptyTitel:(NSString *)emptyTitel
{
    objc_setAssociatedObject(self, @selector(emptyTitel), emptyTitel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)emptyTitel
{
    return objc_getAssociatedObject(self, @selector(emptyTitel));
}

- (void)setEmptyImageName:(NSString *)emptyImageName
{
    objc_setAssociatedObject(self, @selector(emptyImageName), emptyImageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)emptyImageName
{
    return objc_getAssociatedObject(self, @selector(emptyImageName));
}

- (void)setEmptyDataDelegate:(id<UITableViewEmptyDataDelegate>)emptyDataDelegate
{
    objc_setAssociatedObject(self, @selector(emptyDataDelegate), emptyDataDelegate, OBJC_ASSOCIATION_ASSIGN);
}

- (void)setVerticalOffset:(CGFloat)verticalOffset
{
    objc_setAssociatedObject(self, @selector(setVerticalOffset:), @(verticalOffset), OBJC_ASSOCIATION_ASSIGN);
}

- (CGFloat)verticalOffset
{
    NSNumber *ver = objc_getAssociatedObject(self, @selector(setVerticalOffset:));
    return ver.floatValue;
}

- (id<UITableViewEmptyDataDelegate>)emptyDataDelegate
{
    return objc_getAssociatedObject(self, @selector(emptyDataDelegate));
}


@end
